package com.ck.controller.student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ck.domain.Exam;
import com.ck.domain.StudentExam;
import com.ck.service.ExamService;

public class FinishedExamListForStudentController extends AbstractController {
	
	private ExamService examService;

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map model = new HashMap();
		
		Integer studentID = (Integer) request.getSession().getAttribute("studentID");
		
		List<StudentExam> studentExamList = examService.gainFinishedStudentExamForStudent(studentID);
		List<Exam> finishedExamListForStudent = new ArrayList<Exam>();
		
		Iterator iter = studentExamList.iterator();
		while(iter.hasNext()){
			StudentExam studentExam = (StudentExam) iter.next();
			Exam exam = examService.gainExam(studentExam.getExam());
			if(exam.getStatus() == 3){
				finishedExamListForStudent.add(exam);
			}
		}
		
		model.put("finishedExamListForStudent", finishedExamListForStudent);
		
		return new ModelAndView("student/finishedExamListForStudent", model);
	}

}
